package com.keepers.repair.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.widget.EditText;

import com.keepers.repair.R;
import com.keepers.repair.helper.SpHelper;
import com.keepers.repair.model.UserModel;
import com.keepers.repair.net.DataLoader;
import com.keepers.repair.utils.AlertToast;
import com.keepers.repair.utils.DefaultUtils;
import com.keepers.repair.view.TitleBar;

/**
 * auth aboom
 * date 2018/11/10
 */
public class PasswordSettingActivity extends BaseActivity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_password);
        UserModel loginUserInfo = SpHelper.getLoginUserInfo();
        TitleBar titleBar = f(R.id.title_bar);
        titleBar.initializeHeader("修改密码");
        EditText oldPassword = f(R.id.old_password);
        EditText newPassword = f(R.id.new_password);
        EditText newPassword2 = f(R.id.new_password2);
        titleBar.setRightText("保存", v -> {
            String oldP = oldPassword.getText().toString();
            if (TextUtils.isEmpty(oldP)) {
                AlertToast.show("请输入原密码");
                return;
            }

            String newP = newPassword.getText().toString();
            if (TextUtils.isEmpty(newP)) {
                AlertToast.show("请输入新密码");
                return;
            }

            String newp2 = newPassword2.getText().toString();
            if (TextUtils.isEmpty(newp2)) {
                AlertToast.show("请确认密码");
                return;
            }

            if (!newP.equals(newp2)) {
                AlertToast.show("输入密码不一致");
                return;
            }
            rxHelper.pend(DataLoader.setPassword(loginUserInfo.Gid, newP, aBoolean -> {
                if (DefaultUtils.value(aBoolean)) {
                    loginUserInfo.password = newp2;
                    SpHelper.setLoginUserInfo(loginUserInfo);
                    AlertToast.show("修改成功");
                    finish();
                    return;
                }
                AlertToast.show("请稍后重试...");
            }));
        });
    }
}
